package com.book.interceptor;

import com.book.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// 登录拦截器
public class LoginHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 登录成功后，会存在用户的session，以此做判断
        User loginUser = (User) request.getSession().getAttribute("user");

        if(loginUser == null){     // 为空说明未登录
            request.setAttribute("msg", "没有权限，请先登录");
            //把msg信息重定向到首页
            request.getRequestDispatcher("/index").forward(request, response);
            return false; // 不放行
        }else{
            return true;  // 放行
        }
    }


}
